package com.geek.novel.dialog

import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.geek.novel.R
import com.geek.novel.activity.auth.LoginActivity
import com.geek.novel.activity.book.BookDetailActivity
import com.geek.novel.activity.book.BookDownloadActivity
import com.geek.novel.activity.book.ViewerActivity
import com.geek.novel.common.Constant
import com.geek.novel.entity.BookRead
import com.geek.novel.listener.MenuItemInflaterViewListener
import com.geek.novel.services.BookRepository
import com.geek.novel.services.manager.BookDownloadManager
import com.geek.novel.utils.ToastUtils

/**
 * 书籍阅读页面的菜单弹框
 */
class BookReadMenuDialog(val mContext: ViewerActivity) : MenuListDialog(mContext, R.layout.listview_menu_item, ArrayList()) {

    /**导入本地书籍*/
    private val keyMenuitemBookDetail = "BOOK_DETAIL"

    /**加入书架或者移出书架*/
    private val keyMenuitemToggleBookShelf = "BOOK_SHELF"

    /**缓存书籍*/
    private val keyMenuitemBookDownload = "BOOK_DOWNLOAD"

    /**书籍的数据库操作接口*/
    private var bookService = BookRepository.getInstance()

    /**确认框*/
    private lateinit var confirmDialog: ConfirmDialog

    init {
        this.onCreateView()

        initConfirmDialog()
    }

    /**初始化确认弹框*/
    private fun initConfirmDialog(){
        confirmDialog = ConfirmDialog(context)
        confirmDialog.hideDismissBtn()
        confirmDialog.setOnConfirmListener("从头开始缓存") {
            startDownloadBook()
        }
        confirmDialog.setOnConfirm2Listener("从当前章节开始"){
            startDownloadBook(true)
        }
    }


    /**
     * 开始缓存书籍
     * @param isContinue 是否从当前章节开始缓存
     */
    private fun startDownloadBook(isContinue: Boolean=false){
        val book = mContext.book
        if(book == null){
            ToastUtils.toast("缓存失败！书籍对象为空！")
            return
        }
        var crawlerIndex = 0
        if(isContinue){
            crawlerIndex = book.curReadIndex
        }
        //开始缓存
        val result = BookDownloadManager.getInstance().startDownload(book, crawlerIndex)
        if(result){
            ToastUtils.toast("加入缓存队列成功")
        }
        confirmDialog.dismiss()
        //前往缓存列表页面
        mContext.startActivityForResultByAnim(mContext, BookDownloadActivity::class.java)
    }


    /**重写绑定事件的方法，处理本地数据*/
    override fun bindEvent(dialogView: View) {
        list.add(MenuItem(keyMenuitemBookDetail, "书籍详情"))

        //获取书籍信息
        val book = bookService.findBookByBookId(mContext.bookId)
        if(book == null){
            ToastUtils.toast("获取书籍信息为空！请退出重试")
            return
        }

        //加入书架获取移出书架
        val bookShelfMenuItem = this.getBookShelfMenuItem(book)
        list.add(bookShelfMenuItem)

        //本地书籍
        if(!book.local){//本地书籍不需要缓存数据
            list.add(MenuItem(keyMenuitemBookDownload, "缓存全本"))
        }

        this.onItemClickListener = MenuItemClickListener()
        this.inflaterViewListener = MenuItemInflaterViewListener()

        super.bindEvent(dialogView)
    }


    /**加入书架、移出书架的菜单*/
    private fun getBookShelfMenuItem(book: BookRead?): MenuItem{
        val bookShelfName = if(book != null && book.collect)"移出书架" else "加入书架"
        return MenuItem(keyMenuitemToggleBookShelf, bookShelfName)
    }


    /**菜单按钮点击的监听器*/
    inner class MenuItemClickListener: MenuListDialog.MenuItemClickListener{
        override fun onItemClick(item: MenuItem, view: View, position: Int): Boolean {
            when(item.key){
                //跳转到书籍详情
                keyMenuitemBookDetail->{
                    val params = Bundle()
                    params.putString("bookId", mContext.bookId)
                    mContext.startActivityByAnim(context, BookDetailActivity::class.java, params)
                }
                //加入书架或者移出书架
                keyMenuitemToggleBookShelf->{
                    val book = mContext.book
                    bookService.toggleBookshelf(book)
                    val key = keyMenuitemToggleBookShelf
                    updateMenuItem(key, getBookShelfMenuItem(book))
                    val bookId = book!!.bookId!!
                    if(book.collect){
                        mContext.novelService.addReadCount(bookId)
                        ToastUtils.toast("已加入书架")
                    }else{
                        mContext.novelService.minusReadCount(bookId)
                        ToastUtils.toast("已移出书架")
                    }
                }
                //缓存全本
                keyMenuitemBookDownload->{
                    //弹框确认
                    confirmDialog.openDialog("确认缓存《${mContext.book!!.bookName}》吗？")
                }
            }
            return true
        }
    }

}